package xyz.lisys.boot.controller;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.lisys.boot.pojo.DataInfo;
import xyz.lisys.boot.util.ResponseUtil;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;

@Api(value = "数据访问", tags = "数据接口")
@RestController
@RequestMapping("/data")
@Slf4j
public class DataController {

    @Resource
    public JdbcTemplate jdbcTemplate;

    @PostMapping("/select")
    public Object select(@RequestBody DataInfo data) {
        val result = jdbcTemplate.queryForList(
                MessageFormat.format("select * from `{0}`;", data.getTableName())
        );
        return ResponseUtil.ok(result);
    }

    @PostMapping("/insert")
    public Object insert(@RequestBody DataInfo data) {
        val filedNameList = new ArrayList<String>();
        val filedValueList = new ArrayList<>();
        val placeholderList = new ArrayList<String>();

        data.getData().forEach((key, value) -> {
            filedNameList.add("`" + key + "`");
            filedValueList.add(value);
            placeholderList.add("?");
        });

        jdbcTemplate.update(
                MessageFormat.format(
                        "insert into `{0}` ({1})  values ({2});",
                        data.getTableName(),
                        String.join(",", filedNameList),
                        String.join(",", placeholderList)
                ),
                filedValueList.toArray()
        );

        return ResponseUtil.ok(data);
    }

    @PostMapping("/update")
    public Object update(@RequestBody DataInfo data) {
        val filedNameList = new ArrayList<String>();
        val primaryKey = data.getPrimaryKey();
        val primaryValue = data.getData().get(primaryKey).toString();

        data.getData().remove(primaryKey);

        data.getData().forEach((key, value) -> {
            if (value != null) {
                filedNameList.add("`" + key + "` " + "='" + value + "'");
            }
        });

        jdbcTemplate.update(
                MessageFormat.format(
                        "update {0} set {1} where {2}={3};",
                        data.getTableName(),
                        String.join(" , ", filedNameList),
                        primaryKey,
                        primaryValue
                )
        );

        return ResponseUtil.ok(data);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DataInfo data) {
        jdbcTemplate.update(
                MessageFormat.format(
                        "delete from {0} where {1}=?;",
                        data.getTableName(),
                        data.getPrimaryKey()
                ),
                data.getData().get(data.getPrimaryKey())
        );

        return ResponseUtil.ok(data);
    }
}
